import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int rouw=scanner.nextInt();
        int[][] aa =new int[rouw][2];
        for(int i=0;i<rouw;i++){
            aa[i][0] = scanner.nextInt();
            aa[i][1] = scanner.nextInt();
        }

        Solution solution=new Solution();
        System.out.println(solution.eraseOverlapIntervals(aa));
    }
}

 class Solution {
    int o=0;
    public int eraseOverlapIntervals(int[][] intervals) {
        if (intervals.length==0) {return 0;}
        Arrays.sort(intervals,new Comparator<int[]>() {
            @Override
            public  int  compare(int a[], int b []) {
                return a[1]-b[1];

            }
        });

        int end = intervals[0][1];

        for (int i=1;i<intervals.length ;i++) {

            if (end <=intervals[i][0]) {end =intervals[i][1];}
            else{o++; }
        }


        for(int x=0;x<intervals.length;x++) {
            for(int y=0;y<2;y++) {
                System.out.print(intervals[x][y]+" ");
            }
            System.out.print("\n");
        }

        return o;
    }
}
